/**
 * 
 */
package com.mc.file;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Base64;

/**
 * @author w.weizhen
 *
 */
public class Base64Util {

    /**
     * @param args
     */
    public static void main(String[] args) {
    	String path = "D:/hynetic/人脸/thumbnailator/";
    	String file = "2-00.jpg";
        System.out.println(Base64Util.encryptToBase64(path + file));
    }
    
    //将文件转为Base64
    public static String encryptToBase64(String path) {
        if (path == null) {
            return null;
        }
        try {
            byte[] b = Files.readAllBytes(Paths.get(path));
            return Base64.getEncoder().encodeToString(b);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    //将Base64转为文件
    public static String decryptByBase64(String base64, String path) {
        if (base64 == null && path == null) {
            return null;
        }
        try {
            Files.write(Paths.get(path), Base64.getDecoder().decode(base64),StandardOpenOption.CREATE);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return path;
    }
}